<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>轮播图</title>
    <style>
        #slider {
            position: relative;
            width: 600px;
            height: 400px;
            margin: 0 auto;
            overflow: hidden;
        }

        .slide {
            position: absolute;
            top: 0;
            left: 0;
            width: 600px;
            height: 400px;
        }

        .active {
            display: block;
        }

        .inactive {
           display: none;
        }
    </style>
</head>

<body>
    <div class="slider">
        <img class="slide" src="http://rticjscfv.hn-bkt.clouddn.com/blog1682155067260.png" alt="">
        <img class="slide" src="http://rticjscfv.hn-bkt.clouddn.com/blog1682167006817.png" alt="">
        <img class="slide" src="http://rticjscfv.hn-bkt.clouddn.com/blog1682221778695.png" alt="">
    </div>
</body>
<script>
    var slides = document.getElementsByClassName('slide')
    var index = 0

    function showSlide(n) {
        // 隐藏当前的图片
        slides[index].className = 'slide inactive'
        // 显式下一张图片
        index = n
        slides[index].className = 'slide active'
        
    }

    function nextSlide() {
        var next = index + 1
        if (next >= slides.length) {
            next = 0
        }
        showSlide(next)
    }
    // 每隔1秒切换一次图片
    setInterval(function () {
        nextSlide();
    }, 1000);
</script>

</html>